package git.matrix.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import git.matrix.pojo.Bill;
import git.matrix.pojo.Provider;
import git.matrix.service.BillServices;
import git.matrix.service.ProviderServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * 订单 控制器 用户请求--》DispatcherServlet（前段控制器）----》映射（通过扫描，注解添加映射列表）--》依据URL进入控制器的方法
 * --服务--mapper---》mysql---数据--控制器---视图
 * 
 * @author admin
 * 
 */

@Controller
@RequestMapping("bill")
public class BillController {
	@Autowired
    ProviderServices providerServices;// 供应商 服务
	@Autowired
    BillServices billServices;

	/**
	 * 显示订单列表 按照条件 查询
	 * 
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/billlist")
	public String billlist(HttpServletRequest req, Model model) throws UnsupportedEncodingException {
		// 获取页面 查询条件
		//出现乱码---编码方式不一致。。。
		req.setCharacterEncoding("ISO-8859-1");
		String productName = req.getParameter("productName");
		String providerId = req.getParameter("providerId");
		String isPayment = req.getParameter("isPayment");
		System.out.println(productName + "----" + providerId + "---"
				+ isPayment);

		// 向页面传递数据
		model.addAttribute("productName", productName);
		model.addAttribute("providerId", providerId);
		model.addAttribute("isPayment", isPayment);

		// 获取全部的供应商--
		List<Provider> data = providerServices.getProByNameCode("", "");
		// 向页面传递数据
		model.addAttribute("providerList", data);

		// 通过条件查询订单
		 List<Bill> billList =
		 billServices.getBillByProNameProID(productName,providerId,isPayment);
//		List<Bill> billList = billServices.getAllBill();
		model.addAttribute("billList", billList);

		return "bill/billlist";

	}

	/**
	 * 显示添加
	 * 
	 * @return
	 */
	@RequestMapping("billadd")
	public String billadd() {
		return "bill/billadd";

	}

	/**
	 * 查看详情
	 * 
	 * @return
	 */
	@RequestMapping("/billview")
	public String billview(HttpServletRequest req,Model model) {
		//获取  订单  id
		int billid = Integer.valueOf(req.getParameter("billid"));
		//获取订单的详情
		Bill billData = billServices.getBillByID(billid);
		//把数据  --》  页面
		model.addAttribute("bill", billData);
		return "bill/billview";

	}
	
	/**
	 * 更新
	 * @return
	 */
	@RequestMapping("billmodify")
	public String billmodify(HttpServletRequest req,Model model)
	{
		//获取  订单  id
		int billid = Integer.valueOf(req.getParameter("billid"));
		//获取订单的详情
		Bill billData = billServices.getBillByID(billid);
		//把数据  --》  页面
		model.addAttribute("bill", billData);
		
		return "bill/billmodify";
	}

}
